//
//  ScanQRViewController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/9.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "ScanQRViewController.h"
#import "QRCodeReaderView.h"
#import "POSActivationVC.h"
#import "TraditionPOSVC.h"
#import "TraditionSecPOSVC.h"
#import "MinedoPosVC.h"

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ScanQRViewController ()<QRCodeReaderViewDelegate,AVCaptureMetadataOutputObjectsDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate>
{
    QRCodeReaderView * readview;//二维码扫描对象
    BOOL isFirst;//第一次进入该页面
    BOOL isPush;//跳转到下一级页面
}
@property (nonatomic, strong) CIDetector *detector;
@end

@implementation ScanQRViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if (isFirst || isPush) {
        if (readview) {
            [self reStartScan];
        }
    }
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (isFirst) {
        isFirst = NO;
    }
    if (isPush) {
        isPush = NO;
    }
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (readview) {
        [readview stop];
        readview.is_Anmotion = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBarWithTitle:@"扫描条码"];
    [self setupView];
}
- (void)setupView {
    if (readview) {
        [readview removeFromSuperview];
        readview = nil;
    }
    
    isFirst = YES;
    isPush = NO;
    readview = [[QRCodeReaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    readview.is_AnmotionFinished = YES;
    readview.backgroundColor = [UIColor clearColor];
    readview.delegate = self;
    readview.alpha = 0;
    [self.view addSubview:readview];
    [UIView animateWithDuration:0.5 animations:^{
        readview.alpha = 1;
    }completion:^(BOOL finished) {
        
    }];
    
}


#pragma mark - QRCodeReaderViewDelegate
- (void)readerScanResult:(NSString *)result{
    readview.is_Anmotion = YES;
    [readview stop];
    //播放扫描二维码的声音
    SystemSoundID soundID;
    NSString *strSoundFile = [[NSBundle mainBundle] pathForResource:@"noticeMusic" ofType:@"wav"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:strSoundFile],&soundID);
    AudioServicesPlaySystemSound(soundID);
    if ([self.delegate respondsToSelector:@selector(didScanSuccessWithResult:)]) {
        [[AppDelegate sharedAppDelegate] popViewController];
        [self.delegate didScanSuccessWithResult:result];//存放URL的地方
    }else if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[POSActivationVC class]]){
        POSActivationVC *topView = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
        [self.navigationController popViewControllerAnimated:YES];
        [topView didScanSuccessWithResult:result];
    }else if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[TraditionPOSVC class]]){
        TraditionPOSVC *topView = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
        [self.navigationController popViewControllerAnimated:YES];
        [topView didScanSuccessWithResult:result];
    }else if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[TraditionSecPOSVC class]]){
        TraditionSecPOSVC *topView = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
        [self.navigationController popViewControllerAnimated:YES];
        [topView didScanSuccessWithResult:result];
    }else if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[MinedoPosVC class]]){
        MinedoPosVC *topView = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];
        [self.navigationController popViewControllerAnimated:YES];
        [topView didScanSuccessWithResult:result];
    }else{
        [self accordingQcode:result];
        [self performSelector:@selector(reStartScan) withObject:nil afterDelay:1.5];
        
    }
}
#pragma mark - other
- (void)accordingQcode:(NSString *)str{
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}
/**
 重新扫描
 */
- (void)reStartScan{
    readview.is_Anmotion = NO;
    if (readview.is_AnmotionFinished) {
        [readview loopDrawLine];
    }
    [readview start];
}
@end
